/**
Definition for a binary tree node. */
#include<iostream>
#include<vector>
using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int cnt=0,k;    //找到最小节点后，计数开始++，==k后，说明找到第k小的元素
    int ans = 0;
    bool flag = false;  //为true时，中序遍历终止
    void inorder(TreeNode* root){    //排序二叉树的中序遍历 = 有序数组
        if(root == nullptr && flag) 
            return;
        if(root->left!=nullptr)
            inorder(root->left);    //遍历左分支

        (this->cnt)++;
        if(this->cnt == k){
            this->ans = root->val;  //对中间节点处理
            flag = true;
        }
        if(root->right!=nullptr)
            inorder(root->right);   //遍历右分支
        return;
    }

    int kthSmallest(TreeNode* root, int k) {
        this->k = k;
        inorder(root);
        return this->ans;
    }
};
int main(){
    return 0;
}